package br.com.fotoxbh.geracao;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;

import com.fotoloka.util.ParamUtil;


public class Impressao {

	public static void main(String[] args) throws FileNotFoundException, PrintException, IOException {
		File foto = new File("C:\\Users\\Pablo\\Dropbox\\Foto-X\\Fotos Formatura Quimica\\LeiauteHorizontal\\secao_1.jpg");
		
		imprimir(foto);
	}

	public static void imprimir(File foto) throws FileNotFoundException,
			PrintException, IOException {
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
	    pras.add(new Copies(1));
	    pras.add(MediaSizeName.ISO_A6);
	    pras.add(OrientationRequested.LANDSCAPE);
	    PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.JPEG, null);
	    if (pss.length == 0)
	      throw new RuntimeException("No printer services available.");
	    
	    PrintService ps = null;
	    for(PrintService print : pss) {
	    	if(print.getName().contains(ParamUtil.getImpressora())) {
	    		ps = print;
	    		break;
	    	}
	    }
	    if(ps == null) {
	    	System.out.println("Impressora não conectada: " + ParamUtil.getImpressora());
	    	return;
	    }
	    System.out.println("Printing to " + ps);
	    DocPrintJob job = ps.createPrintJob();
	    FileInputStream fin = new FileInputStream(foto);
	    DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG; 
	    Doc doc = new SimpleDoc(fin, flavor, null);
	    job.print(doc, pras);
	    
	    fin.close();
	}
}
